<template>
  <el-dialog
    v-model="dialogFormVisible"
    :title="title"
    width="800px"
    @close="close"
  >
    <el-form ref="formRef" label-width="80px" :model="form" :rules="rules">
      <el-form-item label="分单权限">
        <el-checkbox-group v-model="form.AssignOrderSetIDs">
          <el-checkbox
            v-for="(item, index) in SetSelectList"
            :key="index"
            :disabled="!form.isAssignOrderSet"
            :label="item.label"
            :value="item.value"
          />
        </el-checkbox-group>
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="close">取 消</el-button>
      <el-button type="primary" @click="save">确 定</el-button>
    </template>
  </el-dialog>
</template>

<script>
  import {
    getAssignOrderSetSelect,
    SaveFlowAssignOrderSet,
  } from '@/api/systemsetBusinessFlow'

  export default defineComponent({
    name: 'SystemsetBusinessDocuments',
    emits: ['fetch-data'],
    setup(props, { emit }) {
      const $baseMessage = inject('$baseMessage')

      const state = reactive({
        formRef: null,
        TaskStaffProps: {
          multiple: true,
          checkStrictly: true,
        },
        SetSelectList: [],
        form: {
          roles: [],
        },
        title: '',
        dialogFormVisible: false,
      })

      const showEdit = (row) => {
        if (!row) {
          state.title = '添加'
        } else {
          state.title = '编辑'
          state.form = Object.assign({}, row)
          state.form.AssignOrderSetIDs = JSON.parse(row.AssignOrderSetIDs)
        }
        getSetSelect()
        state.dialogFormVisible = true
      }
      const close = () => {
        state['formRef'].resetFields()
        state.form = {
          roles: [],
        }
        state.dialogFormVisible = false
      }
      const getSetSelect = async () => {
        const {
          data: { AssignOrderSetOption },
        } = await getAssignOrderSetSelect()
        state.SetSelectList = AssignOrderSetOption
      }

      const save = () => {
        state['formRef'].validate(async (valid) => {
          if (valid) {
            const dataMsg = {
              FlowSetID: state.form.FlowSetID,
              AssignOrderSetIDs: state.form.AssignOrderSetIDs,
            }
            const { msg } = await SaveFlowAssignOrderSet(dataMsg)
            $baseMessage(msg, 'success', 'vab-hey-message-success')
            emit('fetch-data')
            close()
          }
        })
      }

      return {
        ...toRefs(state),
        showEdit,
        getSetSelect,
        close,
        save,
      }
    },
  })
</script>
